package project

import (
	"context"
	"fmt"
	"gf-assistant/internal/common/consts"
	"gf-assistant/internal/common/factory"
	"gf-assistant/internal/project/parameter"
	"github.com/gogf/gf/v2/util/gconv"
	"reflect"
)

type TaskService struct{ taskDao string }

func init() {
	taskService := TaskService{
		taskDao: "project.TaskDao",
	}
	factory.Put(reflect.TypeOf(taskService).String(), &taskService)
}
func (s *TaskService) GetTaskDao() *TaskDao {
	return factory.Get(s.taskDao).(*TaskDao)
}
func (s *TaskService) ListTask(ctx context.Context, req *parameter.ListTaskReq) (res *parameter.ListTaskRes) {
	taskRes := s.GetTaskDao().ListTask(ctx, req) //查询数据库，得到指定查询条件的项目任务清单（没有父子结构）
	return &parameter.ListTaskRes{
		Detail: taskRes,
	}
}
func (s *TaskService) CreateTask(ctx context.Context, req *parameter.CreateTaskReq) (res *parameter.CreateTaskRes) {
	req.SetOperatorId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	//开启事务
	tx, err := s.GetTaskDao().DB().Begin(ctx)
	defer func() {
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				return
			}
			fmt.Println("事务回滚")
			panic("创建任务失败")
		} else {
			err := tx.Commit()
			if err != nil {
				return
			}
			fmt.Println("事务提交")
		}
	}()
	res, err = s.GetTaskDao().CreateTask(tx, req)
	return res
}
func (s *TaskService) SaveTask(ctx context.Context, req *parameter.SaveTaskReq) (res *parameter.SaveTaskRes) {
	req.SetOperatorId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	//开启事务
	tx, err := s.GetTaskDao().DB().Begin(ctx)
	defer func() {
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				return
			}
			fmt.Println("事务回滚")
			panic("修改任务失败")
		} else {
			err := tx.Commit()
			if err != nil {
				return
			}
			fmt.Println("事务提交")
		}
	}()
	s.GetTaskDao().SaveTask(tx, req)
	return res
}

func (s *TaskService) DeleteTask(ctx context.Context, req *parameter.DeleteTaskReq) (res *parameter.DeleteTaskRes) {
	req.SetOperatorId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	//开启事务
	tx, err := s.GetTaskDao().DB().Begin(ctx)
	defer func() {
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				return
			}
			fmt.Println("事务回滚")
			panic("删除任务失败")
		} else {
			err := tx.Commit()
			if err != nil {
				return
			}
			fmt.Println("事务提交")
		}
	}()
	s.GetTaskDao().DeleteTask(tx, req)
	return res
}
func (s *TaskService) DistributeTask(ctx context.Context, req *parameter.DistributeTaskReq) (res *parameter.DistributeTaskRes) {
	req.SetOperatorId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	//开启事务
	tx, err := s.GetTaskDao().DB().Begin(ctx)
	defer func() {
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				return
			}
			fmt.Println("事务回滚")
			panic("分配任务失败")
		} else {
			err := tx.Commit()
			if err != nil {
				return
			}
			fmt.Println("事务提交")
		}
	}()
	s.GetTaskDao().DistributeTask(tx, req)
	return res
}
func (s *TaskService) GetTimeLineTask(ctx context.Context, req *parameter.GetTaskTimeLineReq) (res *parameter.GetTaskTimeLineRes) {
	timeLints := s.GetTaskDao().GetTimeLineTask(ctx, req)
	return &parameter.GetTaskTimeLineRes{
		Detail: timeLints,
	}
}
func (s *TaskService) GetMyTask(ctx context.Context, req *parameter.MyTaskReq) (res *parameter.MyTaskRes) {
	req.SetUserId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	tasks := s.GetTaskDao().GetMyTask(ctx, req)
	return &parameter.MyTaskRes{
		Detail: tasks,
	}
}

func (s *TaskService) BeginTask(ctx context.Context, req *parameter.BeginTaskReq) (res *parameter.BeginTaskRes) {
	req.SetUserId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	//开启事务
	tx, err := s.GetTaskDao().DB().Begin(ctx)
	defer func() {
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				return
			}
			fmt.Println("事务回滚")
			panic("开始任务失败")
		} else {
			err := tx.Commit()
			if err != nil {
				return
			}
			fmt.Println("事务提交")
		}
	}()
	s.GetTaskDao().BeginTask(tx, req)
	return res
}

func (s *TaskService) DoneTask(ctx context.Context, req *parameter.DoneTaskReq) (res *parameter.DoneTaskRes) {
	req.SetUserId(gconv.Uint(ctx.Value(consts.ContextKeyUserId)))
	//开启事务
	tx, err := s.GetTaskDao().DB().Begin(ctx)
	defer func() {
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				return
			}
			fmt.Println("事务回滚")
			panic("完成任务失败")
		} else {
			err := tx.Commit()
			if err != nil {
				return
			}
			fmt.Println("事务提交")
		}
	}()
	s.GetTaskDao().DoneTask(tx, req)
	return res
}
